package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryStatisticsBo;
import org.eiahe.hr.salary.domain.dto.SalaryGenerateStatDto;
import org.eiahe.hr.salary.domain.vo.SalaryStatisticsVo;
import org.eiahe.hr.salary.service.ISalaryStatisticsService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 薪资统计Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/statistics")
public class SalaryStatisticsController extends BaseController {

    private final ISalaryStatisticsService iSalaryStatisticsService;

    /**
     * 查询薪资统计列表
     */
    @SaCheckPermission("salary:statistics:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryStatisticsVo> list(SalaryStatisticsBo bo, PageQuery pageQuery) {
        return iSalaryStatisticsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出薪资统计列表
     */
    @SaCheckPermission("salary:statistics:export")
    @Log(title = "薪资统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryStatisticsBo bo, HttpServletResponse response) {
        List<SalaryStatisticsVo> list = iSalaryStatisticsService.queryList(bo);
        ExcelUtil.exportExcel(list, "薪资统计数据", SalaryStatisticsVo.class, response);
    }

    /**
     * 获取薪资统计详细信息
     *
     * @param statisticsId 主键
     */
    @SaCheckPermission("salary:statistics:query")
    @GetMapping("/{statisticsId}")
    public R<SalaryStatisticsVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("statisticsId") Long statisticsId) {
        return R.ok(iSalaryStatisticsService.queryById(statisticsId));
    }

    /**
     * 新增薪资统计
     */
    @SaCheckPermission("salary:statistics:add")
    @Log(title = "薪资统计", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryStatisticsBo bo) {
        return toAjax(iSalaryStatisticsService.insertByBo(bo));
    }

    /**
     * 修改薪资统计
     */
    @SaCheckPermission("salary:statistics:edit")
    @Log(title = "薪资统计", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryStatisticsBo bo) {
        return toAjax(iSalaryStatisticsService.updateByBo(bo));
    }

    /**
     * 删除薪资统计
     *
     * @param statisticsIds 主键串
     */
    @SaCheckPermission("salary:statistics:remove")
    @Log(title = "薪资统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{statisticsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] statisticsIds) {
        return toAjax(iSalaryStatisticsService.deleteWithValidByIds(Arrays.asList(statisticsIds)));
    }

    /**
     * 生成薪资统计
     */
    @SaCheckPermission("salary:statistics:generate")
    @Log(title = "薪资统计", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public R<Void> generate(@RequestBody SalaryGenerateStatDto dto) {
        return toAjax(iSalaryStatisticsService.generateStatistics(dto));
    }
}
